package so.sao.code.service.web.portal;

import java.util.List;

import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import so.sao.auth.autoreg.annotation.ButtonResource;
import so.sao.code.dto.request.CodeSeqment;
import so.sao.code.service.config.Constant;
import so.sao.code.service.infrastructure.enums.ActivationMethod;
import so.sao.code.service.infrastructure.model.vo.RangeActivationVO;
import so.sao.code.service.infrastructure.model.vo.TagDetailVO;
import so.sao.code.service.infrastructure.model.vo.TagStatisticsVO;
import so.sao.code.service.service.RangeActivationService;
import so.sao.sso.bind.CurrentUser;
import so.sao.sso.feign.dto.UserDTO;
import so.sao.top.core.util.rest.RestException;

@Api(tags = "RangeActivation", description = "手工按码段登记激活")
@RestController
@RequestMapping("portal/rangeActivation")
public class RangeActivationController {

	@Autowired
	RangeActivationService rangeActivationService;
	@Autowired
	Constant constant;

	/**
	 * 获取所有的激活动作，用于设置该标签码段的激活条件
	 *
	 * @return
	 */
	@ApiOperation(value = "获取所有激活动作接口", notes = "获取所有激活动作接口")
	@GetMapping("method")
	@RequiresAuthentication
	public ActivationMethod[] findAll() {
		return ActivationMethod.values();
	}

	@ApiOperation(value = "撤销绑定", notes = "撤销绑定")
	@ApiImplicitParam(name = "id", value = "撤销绑定", required = true, dataType = "long", paramType = "body")
	@PostMapping(value = "undoBind")
	@ButtonResource(cls = "", url = "rangeactivationundobind")
	@RequiresPermissions("code.rangeactivationundobind")
	public ResponseEntity<Void> undoBind(@Valid @RequestBody long id) {
		rangeActivationService.undoBind(id);
		return new ResponseEntity<>(HttpStatus.OK);
	}

	/**
	 * 标签操作日志
	 */
	@ApiOperation(value = "标签操作日志", notes = "标签操作日志")
	@ApiImplicitParam(name = "tagBatchId", value = "批次ID", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "detail/{tagBatchId}")
	public ResponseEntity<TagDetailVO> detail(@PathVariable Long tagBatchId) throws Exception {
		TagDetailVO vo = rangeActivationService.detail(tagBatchId);
		return new ResponseEntity<>(vo, HttpStatus.OK);
	}

	@ApiOperation(value = "批次管理中码分段", notes = "批次管理中对码分段")
	@ApiImplicitParam(name = "codeSeqment", value = "码段相关信息", dataType = "CodeSeqment", paramType = "body")
	@PostMapping(value = "doSegment", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ButtonResource(url = "doSegment")
	@RequiresPermissions("code.doSegment")
	public ResponseEntity<RangeActivationVO> doSegment(@Valid @RequestBody CodeSeqment codeSeqment) {
		return new ResponseEntity<>(rangeActivationService.doSegment(codeSeqment), HttpStatus.OK);
	}

	@ApiOperation(value = "批次管理中登记", notes = "批次管理中登记")
	@ApiImplicitParam(name = "codeSeqment", value = "码段相关信息", dataType = "CodeSeqment", paramType = "body")
	@PostMapping(value = "doBind", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ButtonResource(url = "doBind")
	@RequiresPermissions("code.doBind")
	public ResponseEntity<Void> doBind(@Valid @RequestBody CodeSeqment codeSeqment) {
		rangeActivationService.doBind(codeSeqment);
		;
		return new ResponseEntity<>(HttpStatus.OK);
	}

	@ApiOperation(value = "批次管理中码段激活", notes = "批次管理中码段激活")
	@ApiImplicitParam(name = "codeSeqment", value = "码段相关信息", dataType = "CodeSeqment", paramType = "body")
	@PostMapping(value = "doActive", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ButtonResource(url = "doActive")
	@RequiresPermissions("code.doActive")
	public ResponseEntity<Void> doActive(@Valid @RequestBody CodeSeqment codeSeqment) {
		rangeActivationService.doActive(codeSeqment);
		return new ResponseEntity<>(HttpStatus.OK);
	}

	/**
	 * 根据批次id查询已分码段
	 *
	 * @return
	 */
	@ApiOperation(value = "根据批次id查询已分码段信息", notes = "返回批次中已分码段")
	@ApiImplicitParam(name = "tagBatchId", value = "tagBatchId", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "findTagBatchSegmentInfo/{tagBatchId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public List<RangeActivationVO> findTagBatchSegmentInfo(@PathVariable long tagBatchId) {
		return rangeActivationService.findTagBatchSegmentInfo(tagBatchId);
	}

	/**
	 * 根据码段id查询码段绑定信息
	 *
	 * @return
	 */
	@ApiOperation(value = "根据码段id查询码段绑定信息", notes = "根据码段id查询码段绑定信息")
	@ApiImplicitParam(name = "rangId", value = "rangId", required = true, dataType = "Long", paramType = "path")
	@GetMapping(value = "findSegmentInfo/{rangId}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@RequiresAuthentication
	public RangeActivationVO findSegmentInfo(@PathVariable long rangId) {
		return rangeActivationService.findSegmentInfoByRangId(rangId);
	}

	/**
	 * 获取标签统计数据
	 *
	 * @return
	 */
	@ApiOperation(value = " 获取标签统计数据", notes = " 获取标签统计数据")
	@ApiImplicitParam(name = "eseId", value = "企业id", required = true, dataType = "long", paramType = "body")
	@PostMapping(value = "tagStatistics")
	@RequiresAuthentication
	public ResponseEntity<TagStatisticsVO> getTag(@CurrentUser UserDTO user, @Valid @RequestBody long eseId) {
		TagStatisticsVO tagStatisticsVO = rangeActivationService.geTagStatistics(user, eseId);
		return new ResponseEntity<>(tagStatisticsVO, HttpStatus.OK);
	}

	/**
	 * 标签激活任务
	 */
	@ApiOperation(value = "标签激活任务", notes = "标签激活任务")
	@ApiImplicitParam(name = "token", value = "token", required = true, dataType = "String", paramType = "body")
	@PostMapping(value = "task")
	@RequiresGuest
	public void tagActiveTask(@RequestBody String token) {
		if (!token.equals(constant.getTaskToken()))
			throw new RestException(HttpStatus.BAD_REQUEST);
		rangeActivationService.tagActiveTask();
	}

	/**
	 * 设置标签批次激活参数
	 *
	 * @return
	 */
	/*
	 * @ApiOperation(value = "批量设置标签批次激活接口", notes = "批量设置标签批次激活接口")
	 * 
	 * @ApiImplicitParam(name = "rangeActivationRequest", value = "标签批次批量激活请求",
	 * required = true, dataType = "RangeActivationRequest", paramType = "body")
	 * 
	 * @PostMapping(value = "bind")
	 * 
	 * @RequiresAuthentication public ResponseEntity<Void>
	 * saveBatches(@ApiIgnore @CurrentUser UserDTO user,
	 * 
	 * @Valid @RequestBody RangeActivationRequest rangeActivationRequest) {
	 * rangeActivationService.bindTag(user, rangeActivationRequest); return new
	 * ResponseEntity<>(HttpStatus.OK); }
	 */

	/**
	 * 批量设置标签批次激活参数
	 *
	 * @return
	 */
	/*
	 * @ApiOperation(value = "批量设置标签批次激活接口", notes = "批量设置标签批次激活接口")
	 * 
	 * @ApiImplicitParam(name = "rangeActivationRequest", value = "标签批次批量激活请求",
	 * required = true, dataType = "RangeActivationRequest", paramType = "body")
	 * 
	 * @PostMapping(value = "mulitBind")
	 * 
	 * @RequiresAuthentication public ResponseEntity<Void>
	 * mulitBind(@ApiIgnore @CurrentUser UserDTO user,
	 * 
	 * @Valid @RequestBody RangeActivationRequest rangeActivationRequest) {
	 * rangeActivationService.mulitBindTag(user, rangeActivationRequest); return new
	 * ResponseEntity<>(HttpStatus.OK); }
	 */
}
